package com.cloudsigns.factorymanager2.produce.adapter.produce_team;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.bean.request.DailyFeedbackDateItem;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceTeamOrderDailyFeedbackDateAdapter extends BaseAdapter {
    private Context context;
    private List<DailyFeedbackDateItem> dailyFeedbackDateItemList = new ArrayList<>();
    private ItemClickListener itemClickListener;
    private IFeedbackDailyItemListener feedbackDailyItemListener;

    public ProduceTeamOrderDailyFeedbackDateAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<DailyFeedbackDateItem> list) {
        if (null != list) {
            this.dailyFeedbackDateItemList.clear();
            this.dailyFeedbackDateItemList.addAll(list);
        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setDailyItemClickListener(IFeedbackDailyItemListener feedbackDailyItemListener) {
        this.feedbackDailyItemListener = feedbackDailyItemListener;
    }

    @Override
    public int getCount() {
        return dailyFeedbackDateItemList.size();
    }

    @Override
    public DailyFeedbackDateItem getItem(int position) {
        return dailyFeedbackDateItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_produce_team_order_daily_feedback, null);
            holder.tv_date = convertView.findViewById(R.id.tv_date);
            holder.tv_week = convertView.findViewById(R.id.tv_week);
            holder.img_add_feedback = convertView.findViewById(R.id.img_add_feedback);
            holder.lv_feedback = convertView.findViewById(R.id.lv_feedback);

            holder.adapter = new ProduceTeamOrderDailyFeedbackAdapter(context, 1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DailyFeedbackDateItem dateItem = dailyFeedbackDateItemList.get(position);
        holder.tv_date.setText(dateItem.getStrDate());
        holder.tv_week.setText(DateUtil.getWeekByDateStr(dateItem.getStrDate()));


        long currentMill = System.currentTimeMillis();
        String currentDate = DateUtil.getYyyy_Mm_Dd(currentMill);
        if (dateItem.getStrDate().equals(currentDate)) {
            holder.img_add_feedback.setImageResource(R.mipmap.icon_add_feedback_pressed);
            holder.img_add_feedback.setEnabled(true);
        } else {
            holder.img_add_feedback.setImageResource(R.mipmap.icon_add_feedback_normal);
            holder.img_add_feedback.setEnabled(false);
        }

        holder.adapter.setData(dateItem.getWorkPartDailyFeedbackBeanList());
        holder.lv_feedback.setAdapter(holder.adapter);

        // 添加反馈
        holder.img_add_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(view, position);
                }
            }
        });
        holder.lv_feedback.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (feedbackDailyItemListener != null) {
                    if (dailyFeedbackDateItemList.get(position).workPartDailyFeedbackBeanList != null &&
                            i < dailyFeedbackDateItemList.get(position).workPartDailyFeedbackBeanList.size()) {
                        feedbackDailyItemListener.onDailyItemClick(dailyFeedbackDateItemList.get(position).workPartDailyFeedbackBeanList.get(i));
                    }
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView tv_date;
        TextView tv_week;
        ImageView img_add_feedback;
        ListViewForScrollView lv_feedback;

        ProduceTeamOrderDailyFeedbackAdapter adapter;
    }
}
